﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using myomscl.DataObjects;
using System.Text;
using System.Globalization;
using myomscl.Utils;

namespace Workflow.Alerts
{
    public class RejectViewPresenter : ActionPresenter
    {
        #region members
        private readonly IRejectView view;
        #endregion

        #region constructor
        public RejectViewPresenter(IRejectView _view)
            : this(_view, null)
        { }

        public RejectViewPresenter(IRejectView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IRejectView>();
            view.RejectLoad += view_OnLoad;
            view.RejectSave += view_OnSave;
        }
        #endregion

        #region private methods

        private void view_OnLoad()
        {
            //var changedAlert = Changes.FindByid(view.AlertId);
            //if (changedAlert != null &&
            //    ((changedAlert.newstatus == (int)WorkflowEnums.Status.Rejected) ||
            //    (changedAlert.newstatus == (int)WorkflowEnums.Status.Declined)))
            //{
            //    view.Comments = changedAlert.comments;
            //}

            List<Alert> tempChanges = TempChanges;
            var alert = tempChanges.Where(a => a.id == view.AlertId).FirstOrDefault();
            if (alert != null)
            {
                var comments = alert.Comments1.OrderBy(c => c.createddatetime).FirstOrDefault();
                if (comments != null)
                    view.Comments = comments.comment;
            }
        }

        private void view_OnSave()
        {

            using (WorkflowEntitySet context = new WorkflowEntitySet())
            {
                List<Alert> tempChanges = TempChanges;
                var alert = tempChanges.Where(a => a.id == view.AlertId).FirstOrDefault();
                if (alert != null)
                    tempChanges.Remove(alert);

                alert = context.Alert.Where(a => a.id == view.AlertId).First();
                string item = Utils.Utils.GetCurrentItem();
                if (item == WorkflowConstants.NewAlerts)
                {
                    alert.currentstatus = (int)WorkflowEnums.Status.Rejected;
                }
                else
                {
                    alert.currentstatus = (int)WorkflowEnums.Status.Declined;
                }

                var comments = new Comments()
                {
                    Person=context.Person.Where(p=>p.id==CurrentUser.Id).First(),
                    createddatetime=DateTime.Now,
                    comment=view.Comments                    
                };
                alert.Comments1.Add(comments);
                tempChanges.Add(alert);
                TempChanges = tempChanges;
            }

            //WorkflowEnums.Status newStatus;
            //WorkflowEnums.Status oldStatus;
            //string item = Utils.Utils.GetCurrentItem();
            //if (item == WorkflowConstants.NewAlerts)
            //{
            //    newStatus = WorkflowEnums.Status.Rejected;
            //    oldStatus = WorkflowEnums.Status.Sent;
            //    UpdateStatus(view.AlertId, newStatus, oldStatus, view.Comments);
            //}
            //else
            //{
            //    newStatus = WorkflowEnums.Status.Declined;
            //    oldStatus = WorkflowEnums.Status.Executed4Municipality;
            //    UpdateStatus(view.AlertId, newStatus, oldStatus, view.Comments);
            //    //var alert = Changes.FindByid(view.AlertId) as EstateDataSet.AlertChangesRow;
            //    //if (alert != null)
            //    //{
            //    //    MainListDAL dal = new MainListDAL();
            //    //    dal.UpdateAlerts(alert, CurrentUser.Id);
                    
            //    //    //AlertViewDAL dtlsDal = new AlertViewDAL();
            //    //    var filter = new WorkflowFilter
            //    //    {
            //    //        AlertId = view.AlertId
            //    //    };

            //    //    var filterSql = new WorkflowFilterSql(filter);
            //    //    //EstateDataSet.AlertDetailsDataTable detailsTable = dtlsDal.GetAlertDetails(filterSql);
            //    //    //bind properties to table fields

            //    //    var contactAlerts = dal.GetContactAlerts(filterSql);

            //    //    if (contactAlerts.Rows.Count == 1)
            //    //    {
            //    //        EstateDataSet.ContactAlertsRow contact = contactAlerts[0];
            //    //        //Session["no_email"] = details.noemail;
            //    //        //Session["mun_email"] = details.munemail;
            //    //        //Session["municipalityname"] = details.municipalityname;

            //    //        Notification.SendNotification(alert.id.ToString(), alert.newstatus , contact);
            //    //    }
            //    //}
            //}
        }
        #endregion
    }
}
